<?php

require_once(R_P.'require/class_content.php');
require_once(D_P.'data/cache/cate.php');
require_once(R_P.'require/chinese.php');

class Publish extends Content {

	protected $mid;

	public function __construct($mid = 1)
	{
		$this->mid = $mid;
	}

	protected function getAddAction()
	{
		require_once(R_P.'require/class_cate.php');
		$cate = new Cate();
		$cate_select = $cate->tree();
		$cate_select = str_replace("value=\"$cid\"","value=\"$cid\" selected",$cate_select);
		$inputArea = $this->inputArea();
		require_once(R_P . 'require/init.php');
		start($very['charset']);
		require Template();
		footer();
	}

}

class Edit extends Content {

	var $mid;
	var $cid;

	function __construct(){
		global $action;
		!$action && $action='add';

		if (in_array($action,array('add','edit'))) $this->show();
		$this->selectMethod($action);
	}

	function Edit(){
		$this->__construct();
	}

	function show(){
		global $action,$cid,$timestamp,$catedb;
		$defaultcid = GetCookie('defaultcid');
		/*require_once(R_P.'require/class_cate.php');
		$cate = new Cate();
		$cate_select = $cate->tree();*/
		if(function_exists('adminmsg') && !$defaultcid && !$cid){
			require PrintEot('header');
			require PrintEot('edit');
			adminbottom();
		}
		!$cid && $cid = $defaultcid;

		if($catedb[$cid]['mid']<0){
			Showmsg('pub_cannotadd'); //调用类型的模型不能添加内容
		}elseif ($catedb[$cid]['mid']==0){
			Showmsg('pub_linkadd'); //外部栏目无法添加内容
		}else{
			Cookie('defaultcid',$cid,$timestamp+31536000);
		}
		/*
		$cate_select = str_replace("value=\"$cid\"","value=\"$cid\" selected",$cate_select);
		$catename = $catedb[$cid]['cname'];*/
		$this->mid = $catedb[$cid]['mid'];
		$this->cid = $cid;
		$this->catedb = $catedb;
	}

	function selectMethod($action){
		switch ($action){
			case 'add':
				$this->addContent();
				break;
			case 'edit':
				$this->editContent();
				break;
			case 'addimage':
				$this->addImage();
				break;
			case 'upload': //编辑的时候上传附件
			$this->uploadAttach();
			break;
			case 'fckupload':
				$this->FckUpload();
				break;
			case 'tags':
				$this->tags();
				break;
			case 'SearchTag':
				$this->SearchTag();
				break;
			case 'uploadRes':
				$this->uploadRes();
				break;
			case 'removeRes':
				$this->removeRes();
				break;
			default:
				$this->addContent();
				break;
		}
	}

	function addContent(){
		global $step,$action,$basename;
		parent::__construct($this->mid);
		$cid = $this->cid;
		if(!$step){
			require_once(R_P.'require/class_cate.php');
			$cate = new Cate();
			$cate_select = $cate->tree();
			$cate_select = str_replace("value=\"$cid\"","value=\"$cid\" selected",$cate_select);
			$inputArea = $this->inputArea();
			require PrintEot('header');
			require PrintEot('edit');
			adminbottom();
		}elseif ($step==2){
			empty($_POST['title']) && Showmsg('pub_notitle');
			$resTitle = $_POST['restitle'];
			$tid = $this->InsertData($_POST, $this->cid);
			$uploadNum = $this->uploadRes($resTitle, $tid);
			if ($uploadNum == 0)
			{
				adminmsg('内容信息添加成功，但未能成功上传资源文件');
			}
			else
			{
				adminmsg('pub_addok');
			}
		}
	}

	function editContent(){
		global $step,$tid,$admin_file,$action,$basename;
		$cid = $this->cid;
		parent::__construct($this->mid);
		if(!$_POST['step'])
		{
			require_once(R_P . 'require/class_resource.php');
			$r = new Resource();
			$res = $r->get($tid);
			require_once(R_P.'require/class_cate.php');
			$cate = new Cate();
			$cate_select = $cate->tree();
			$cate_select = str_replace("value=\"$cid\"","value=\"$cid\" selected",$cate_select);
			$inputArea = $this->editArea($tid);
			list($tagsid,$tags) = $this->getTags($tid);
			$aids = $this->getAttach($tid);
			$aids = implode(',',$aids);
			require PrintEot('header');
			require PrintEot('edit');
			adminbottom();
		}
		elseif ($_POST['step']==2)
		{
			empty($_POST['title']) && Showmsg('pub_notitle');
			$resTitle = $_POST['restitle'];
			$uploadNum = $this->uploadRes($resTitle, $tid);
			$this->UpdateData($_POST,$_POST['tid']);
			/*
			if($GLOBALS['catedb'][$cid]['htmlpub']){
			$tid = intval($_POST['tid']);
			adminmsg('pub_edithtmlok',"$admin_file?adminjob=content&action=whole&cid=$cid&tid=$tid&job=pubupdate");
			}else{
			*/
			adminmsg('pub_editok',"$admin_file?adminjob=content&cid=$this->cid&action=view");
			//}
		}
	}

	function addImage(){ //插入附件资源
		global $inputname,$inputtype,$basename,$type,$page,$very;
		require_once R_P.'require/class_attach.php';
		$attach = new Attach();
		$attach->displaynum = 15;
		$files = $attach->show($page,$type);
		$pages = $attach->pages;
		require PrintEot('header');
		require PrintEot('edit_addimage');
		adminbottom(0);
	}

	function uploadAttach(){
		global $basename,$inputname,$inputtype;
		require_once(R_P.'require/class_attach.php');
		$attach = new Attach();
		$attach->upload();
		echo '<script type="text/javascript">' ;
		echo 'alert("Your file has been successfully uploaded!");';
		echo 'parent.reload.click()';
		echo '</script>' ;
		exit ;
		//adminmsg('pub_uploadok',$basename."&inputname=$inputname&inputtype=$inputtype&action=addimage");
	}

	function uploadRes($resTitle, $tid = '')
	{
		require_once(R_P . 'require/class_resource.php');
		$res = new Resource();
		return $res->upload($resTitle, $tid);
	}

	protected function removeRes()
	{
		$rid = intval($_GET['rid']);
		require_once(R_P . 'require/class_resource.php');
		$res = new Resource();
		echo $res->remove($rid);
		exit;
	}

	/**
	 * Fck编辑器的专用上传
	 *
	 */
	function FckUpload(){
		global $basename,$inputname,$inputtype;
		require_once(R_P.'require/class_attach.php');
		$attach = new Attach();
		$attach->upload();
		$errorNumber = 0;
		$fileName = $attach->fileName;
		$fileUrl = $GLOBALS['very']['attachdir'].'/'.$attach->filePath;
		echo '<script type="text/javascript">' ;
		echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . str_replace( '"', '\\"', $fileUrl ) . '","' . str_replace( '"', '\\"', $fileName ) . '", "' . str_replace( '"', '\\"', $customMsg ) . '") ;' ;
		echo '</script>' ;
		exit ;
	}

	function tags(){
		global $db,$tagname;
		$tagname = $this->Convert($tagname);
		$tagname = Char_cv($tagname);
		$tagname = explode(' ',$tagname);
		$tags = '';
		foreach($tagname as $tag){
			if(!$tag){continue;}
			$rs = $db->get_one("SELECT tagid FROM cms_tags WHERE tagname='$tag'");
			if($rs){
				$tags .= $tags ? ','.$rs['tagid'] : $rs['tagid'];
			}else{
				$db->update("INSERT INTO cms_tags SET tagname='$tag'");
				$tags .= $tags ? ','.$db->insert_id() : $db->insert_id();
			}
		}
		echo $tags;
		exit();
	}

	function SearchTag(){
		global $db,$tagname;
		$tagname = Char_cv($tagname);
		$tagname = trim(str_replace(array('%','_'),array('\%','\_'),$tagname));
		$rs = $db->query("SELECT * FROM cms_tags WHERE tagname LIKE('$tagname%') ORDER BY num DESC LIMIT 0,10");
		$tmpText = "";
		while ($tag = $db->fetch_array($rs)){
			$i++;
			$tmpText .= "<A href=\"javascript:SelectTag('{$tag[tagname]}','{$tag[tagid]}');\" id=\"tag_{$tag[tagid]}\">{$tag[tagname]}(<font color=red>{$tag[num]}</font>)</A>&nbsp;&nbsp;&nbsp;";
			$i==5 && $tmpText .= '<br/>';
		}
		if($tmpText){
			echo $tmpText;
		}else{
			echo 'error';
		}
		exit();
	}

	function Convert($str){
		global $charset;
		if($charset != 'utf8'){
			$chs = new Chinese('UTF8',$charset);
			$str=$chs->Convert($str);
		}
		return $str;
	}
}

?>